#include "DataShowPage.h"
#include "ui_DataShowPage.h"

DataShowPageWidget::DataShowPageWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    m_CPageCtrl = nullptr;
    m_rightWdiget = nullptr;
    m_leftWdiget = nullptr;
    m_pPersonList = nullptr;
    m_pSearch =nullptr;
    Init();
}

DataShowPageWidget::~DataShowPageWidget()
{
    if(m_pSearch)
    {
        delete m_pSearch;
        m_pSearch = nullptr;
    }
    if(m_pPersonList)
    {
        delete m_pPersonList;
        m_pPersonList = nullptr;
    }

    if(m_CPageCtrl)
    {
        delete m_CPageCtrl;
        m_CPageCtrl = nullptr;
    }

    if(m_leftWdiget)
    {
        delete m_leftWdiget;
        m_leftWdiget = nullptr;
    }

    if(m_rightWdiget)
    {
        delete m_rightWdiget;
        m_rightWdiget = nullptr;
    }

    delete ui;


}

void DataShowPageWidget::Init()
{
    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(0); //间距
    mainLayout->setContentsMargins(0,0,0,0);

    m_leftWdiget = new QWidget(this);
    m_rightWdiget = new QWidget(this);

    m_leftWdiget->setMaximumWidth(300);
    mainLayout->addWidget(m_leftWdiget);
    mainLayout->addWidget(m_rightWdiget);

    QVBoxLayout* vboxLayout = new QVBoxLayout(m_leftWdiget);
    vboxLayout->setSpacing(0); //间距
    vboxLayout->setContentsMargins(0,0,0,0);

    //搜索
    m_pSearch = new CSearchCtrl(this);
    vboxLayout->addWidget(m_pSearch);
    connect(m_pSearch, &CSearchCtrl::SearchClicked, this, &DataShowPageWidget::OnSearchClicked);

    m_pPersonList = new QListView();
    m_pPersonList->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    m_pPersonList->setSelectionMode(QAbstractItemView::SingleSelection);//单选模式
    m_pPersonList->setStyleSheet(
        R"(
            QListView {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }

            QListView::item {
                height: 30px; /* 统一行高 */
            }
        )"
        );

    vboxLayout->addWidget(m_pPersonList);



    //分页
    m_CPageCtrl = new CPageCtrl();
    connect(m_CPageCtrl, &CPageCtrl::PageNumChange, this,&DataShowPageWidget::OnPageNumChange);
    m_CPageCtrl->SetCurPageNum(1);
    vboxLayout->addWidget(m_CPageCtrl);

    connect(&NetWorkMgr::instance(), &NetWorkMgr::GetPersonInfoListDone, this, &DataShowPageWidget::OnGetPersonInfoListDone);

    //初始获取数据
    GetPersonInfoList();
}

void DataShowPageWidget::OnPageNumChange(const int & nPageNum)
{

    //获取页数
    GetPersonInfoList();
}

void DataShowPageWidget::GetPersonInfoList(int nCurPage)
{
    if(!m_CPageCtrl || !m_pSearch)
    {
        return;
    }

    QString strsearchtext;
    m_pSearch->GetSearchText(strsearchtext);


    QJsonObject jsParam;
    jsParam["limit"] = 20;
    if(nCurPage == 0)
    {
        jsParam["page"] = m_CPageCtrl->GetCurPageNum();
    }
    else
    {
        jsParam["page"] = nCurPage;
    }


    if(!strsearchtext.isEmpty())
    {
        jsParam["name"] = strsearchtext;
    }

    qDebug() << "GetPersonInfoListDone:" << QString(QJsonDocument(jsParam).toJson());
    NetWorkMgr::instance().GetPerosnInfoList(jsParam, "0");
}


void DataShowPageWidget::OnGetPersonInfoListDone(const QJsonObject &jsResult)
{

    //设置给控件
    qDebug() << "OnGetPersonInfoListDone:" << QString(QJsonDocument(jsResult).toJson());

    if(!jsResult.contains("bsuccess") ||
        !jsResult.contains("result") || !jsResult["result"].isObject())
    {
        return;
    }

    if(jsResult["bsuccess"].toBool() != true)
    {
        qDebug() << "111";
        return;
    }

    const QJsonObject & jsresult = jsResult["result"].toObject();


    if(!jsresult["list"].isArray())
    {
        qDebug() << "222";
        return;
    }

    if(!m_pPersonList || !m_CPageCtrl)
    {
        qDebug() << "333";
        return;
    }


    int ntotalcount = jsresult["total"].toInt();
    int ntotalpagecount = jsresult["totalpage"].toInt();
    int ncurpagenum = jsresult["page"].toInt();



    m_CPageCtrl->SetTotalCount(ntotalpagecount, ntotalcount, ncurpagenum);



    // 创建带图标的项
    //QStandardItem *item1 = new QStandardItem(QIcon("icon.png"), "Item 1");
    //QStandardItem *item2 = new QStandardItem("Item 2");

    QStandardItemModel *model = new QStandardItemModel(this);

    //设置给listview
    const QJsonArray & jsarr = jsresult["list"].toArray();
    for(int i = 0; i < jsarr.size(); ++i)
    {
        QJsonValue value = jsarr.at(i);
        if(value.isString())
        {
            qDebug() << "字符串:" << value.toString();
        }
        else if(value.isObject())
        {
            QJsonObject jstemp = value.toObject();
            QString strname =  jstemp["name"].toString();
            QStandardItem *item1 = new QStandardItem(strname);
            model->appendRow(item1);
        }

    }

    m_pPersonList->setModel(model);
}

void DataShowPageWidget::OnSearchClicked()
{
    //每次搜索设置当前页数为1
    GetPersonInfoList(1);
}
